//
//  ConfirmCouponCodeView.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit

class ConfirmCodeView: UIView {
    var descLab1: UILabel!
    var descLab2: UILabel!
    var statusLab: UILabel!

    var signLab: UILabel!
    var inputTF: UITextField!
    override init(frame: CGRect) {
        super.init(frame: frame)
        inputTF = UITextField()
        inputTF.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        inputTF.backgroundColor = UIColor.init(hex: 0xF7F8FB)
        inputTF.layer.cornerRadius = 4
        inputTF.layer.masksToBounds = true
        inputTF.clearButtonMode = .whileEditing
        inputTF.placeholder = MMLocalizedString("confirm_input_code", comment: "请输入优惠码")
        let leftModel = UIView(frame: CGRect.init(x: 0, y: 0, width: 14, height: 14))
        inputTF.leftView = leftModel
        inputTF.leftViewMode = .always
        self.addSubview(inputTF)
        
        descLab1 = UILabel()
        descLab1.textColor = Theme.mainColor
        descLab1.backgroundColor = UIColor.clear
        descLab1.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        descLab1.numberOfLines = 1
        descLab1.lineBreakMode = .byTruncatingTail
        descLab1.textAlignment = .left
        //descLab1.text = "满1000减200"
        self.addSubview(descLab1)
        
        descLab2 = UILabel()
        descLab2.textColor = Theme.mainColor
        descLab2.backgroundColor = UIColor.clear
        descLab2.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        descLab2.numberOfLines = 1
        descLab2.lineBreakMode = .byTruncatingTail
        descLab2.textAlignment = .left
        //descLab2.text = "优惠券说明，优惠券说明惠券说明"
        self.addSubview(descLab2)

        signLab = UILabel()
        signLab.textColor = Theme.titleLabColor
        signLab.backgroundColor = UIColor.init(hex: 0xF7F8FB)
        signLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        signLab.numberOfLines = 1
        signLab.lineBreakMode = .byTruncatingTail
        signLab.textAlignment = .center
        //signLab.text = "仅商品可用"
        signLab.layer.borderWidth = 1
        signLab.layer.borderColor = Theme.titleLabColor.cgColor
        signLab.layer.cornerRadius = 8
        signLab.layer.masksToBounds = true
        self.addSubview(signLab)
        
        statusLab = UILabel()
        statusLab.textColor = Theme.redColor
        statusLab.backgroundColor = UIColor.clear
        statusLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        statusLab.numberOfLines = 1
        statusLab.lineBreakMode = .byTruncatingTail
        statusLab.textAlignment = .left
        //statusLab.text = "优惠码无法使用"
        self.addSubview(statusLab)
        
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        inputTF.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(10)
            make.right.equalTo(-20)
            make.height.equalTo(44)
        }
        
        descLab1.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(inputTF.snp.bottom).offset(8)
            make.right.equalTo(-10)
        }
        
        descLab2.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-10)
            make.top.equalTo(descLab1.snp.bottom).offset(4)
        }
        
        signLab.sizeToFit()
        let tempW = signLab.frame.size.width + 20
        signLab.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(descLab2.snp.bottom).offset(10)
            make.width.equalTo(tempW)
            make.height.equalTo(20)
        }
        
        statusLab.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(signLab.snp.bottom).offset(8)
            make.right.equalTo(-10)
            make.bottom.equalTo(-20).priority(.high)
        }
        
        self.layoutIfNeeded()
    }
    
    func reloadLabelUpdate(){
        if signLab.text == "" {
            signLab.isHidden = true
            signLab.snp.updateConstraints { make in
                make.height.equalTo(0)
                make.top.equalTo(descLab2.snp.bottom).offset(0)
            }
            
        }else{
            signLab.sizeToFit()
            let tempW = signLab.frame.size.width + 20
            signLab.isHidden = false
            signLab.snp.updateConstraints { make in
                make.top.equalTo(descLab2.snp.bottom).offset(10)
                make.height.equalTo(20)
                make.width.equalTo(tempW)
            }
        }
        
        
        self.updateConstraints()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
